import Food from "./Food";
import ScorePanel from "./ScorePanel";
import Snake from "./Snake";
//枚举 空格键 方向键与 keycode
//数值型枚举
enum Keyboard {
  Space = 32,
  Left = 37,
  Up = 38,
  Right = 39,
  Down = 40,
}
export default class GameController {
  //定义三个属性
  snake: Snake;
  food: Food;
  scorePanel: ScorePanel;
  keyboards: Keyboard;
  //推荐**字面量类型+联合类型组合的方式**，因为相比枚举，这种方式更加直观、简洁、高效。
  // direction: "up" | "down" | "left" | "right";

  //是否存活
  isLived = true;
  constructor() {
    this.snake = new Snake();
    this.food = new Food();
    /**
     * 可传参数: 最高等级  升级所需分数
     * @param {number} maxLevel
     * @param {number} upScore
     */
    this.scorePanel = new ScorePanel(10,1);
    // this.direction = "right";
    this.keyboards = Keyboard.Space;
    this.init(); //创建后立即开始
    console.log(this.keyboards);
  }

  //游戏初始化
  init() {
    //绑定键盘按下的事件
    document.addEventListener("keydown", this.keydownHandler.bind(this)); //把this绑定到函数自身不会因为谁调用就改成谁
    this.run();
  }
  //键盘按下响应函数
  keydownHandler(e: KeyboardEvent) {
    //赋值前得判断在不在其中
    switch (e.keyCode) {
      case Keyboard.Up:
        this.keyboards = Keyboard.Up;
        break;
      case Keyboard.Down:
        this.keyboards = Keyboard.Down;
        break;
      case Keyboard.Left:
        this.keyboards = Keyboard.Left;
        break;
      case Keyboard.Right:
        this.keyboards = Keyboard.Right;
        break;
      case Keyboard.Space:
        this.keyboards = Keyboard.Space;
        break;
      default:
        break;
    }

    console.log("e", e);
    console.log("this.keyboards", this.keyboards);
    // key: " "keyCode: 32  空格
    //key: "ArrowUp" keyCode: 38
    //e.key ArrowLeft
    // GameController.ts:39 e.key ArrowUp
    // GameController.ts:39 e.key ArrowRight
    // GameController.ts:39 e.key ArrowDown
  }

  //蛇运动的方法
  run() {
    /**
     *  Up -> top --
     *  Down   -> top++
     *  Left   -> left --
     *  Right   -> left ++
     */
    //获取蛇现在的坐标
    let X = this.snake.X;
    let Y = this.snake.Y;
    switch (this.keyboards) {
      case Keyboard.Up:
        Y -= 10;
        break;
      case Keyboard.Down:
        Y += 10;
        break;
      case Keyboard.Left:
        X -= 10;
        break;
      case Keyboard.Right:
        X += 10;
        break;
      default:
        break;
    }
    //检测是否吃到食物
    // if ( this.checkEat(X,Y)) {
    //   console.log('蛇吃到了');
    //   this.snake.addBody();
    //   this.food.change();
    //   this.scorePanel.addScore();
    // }
    this.checkEat(X, Y);
    //蛇移动
    try {
      this.snake.X = X;
      this.snake.Y = Y;
    } catch (e) {
      //捕获异常弹出提示信息
      alert(e);
      this.isLived = false;
    }

    this.isLived &&
      setTimeout(this.run.bind(this), 300 - (this.scorePanel.level - 1) * 30); //自己调用自己  速度根据等级来改变
  }

  //蛇吃东西
  checkEat(X: number, Y: number) {
    // return X === this.food.X && Y === this.food.Y;
    if (X === this.food.X && Y === this.food.Y) {
      console.log('蛇吃到了');
      this.snake.addBody();
      this.food.change();
      this.scorePanel.addScore();
    }
  }
}
